package com.bowen.springcloud.controller;


import com.bowen.springcloud.entity.CommonResult;
import com.bowen.springcloud.entity.Payment;
import com.bowen.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @ProjectName: cloud2020
 * @Package: com.bowem.springcloud.controller
 * @ClassName: PaymentController
 * @Author: Bowen
 * @Description:
 * @Date: 2020/3/20 20:23
 * @Version: 1.0.0
 */
@RestController
@Slf4j
public class PaymentController {

    @Resource
    private PaymentService paymentService;

    @Value("${server.port}")
    private String serverport;

    @PostMapping(value = "/payment/creat")
    public CommonResult creat(@RequestBody Payment payment) {
        int creat = paymentService.create(payment);
        if (creat > 0) {
            return new CommonResult(200, "插入数据成功" + serverport, creat);
        } else {
            return new CommonResult(444, "插入数据失败", creat);
        }
    }

    @GetMapping(value = "/payment/get/{id}")
    public CommonResult getPamentId(@PathVariable("id") Long id) {
        log.info("111111");
        Payment pamentId = paymentService.getPaymentById(id);
        if (pamentId != null) {
            return new CommonResult(200, "查询成功" + serverport, pamentId);
        } else {
            return new CommonResult(444, "没有查询ID:" + id, null);
        }
    }

    @GetMapping(value = "/payment/lb")
    public String getPaymentLB() {
        return serverport;
    }

    //OpenFeign超时控制演示
    //OpenFeign默认等待1秒钟，超过则报错
    @GetMapping(value = "/payment/feign/timeout")
    public String paymentFeignTimeout() {
        //暂停几秒钟线程
        try{
            TimeUnit.SECONDS.sleep(3);
        }catch(InterruptedException e) {
            e.printStackTrace();
        }
        return serverport;
    }
}
